﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
//using PCSUtils.Utils;
//using PCSComUtils.Common;
using PCSComProduct.Items.BO;
using PCSComUtils.DataContext;
using PCSComUtils.Common.DS;

using C1.Win.C1Input;
using C1.Win.C1TrueDBGrid;
using PCSComUtils.Common;
using PCSComUtils.PCSExc;
using PCSUtils.Log;
using PCSUtils.Utils;
using CancelEventArgs = System.ComponentModel.CancelEventArgs;

namespace PCSProduct.Items
{
    public partial class ImportExel : Form
    {
        const string THIS = "PCSProduct.Items.ImportExel";

        /// <summary>
        /// Required designer variable.
        /// </summary>
        //private Container components = null;
        bool iCheckCopy = false;
        private string strExcelFilename = "";
        private ExcelReader objExcelReader = null;
        private DataTable dtbData;
        private DataTable dtbGridLayout = new DataTable();
        //ImportPlanDataBO boImport = new ImportPlanDataBO(); 
        List<ItemVO> _items = new List<ItemVO>();
        private EnumAction mFormAction = EnumAction.Default;
        DataSet dset;
        public ImportExel()
        {
            InitializeComponent();
        }

        private void ImportExel_Load(object sender, EventArgs e)
        {
            const string METHOD_NAME = THIS + ".ImportExel_Load()";
            try
            {
                //Set authorization for user
                Security objSecurity = new Security();
                this.Name = THIS;
                if (objSecurity.SetRightForUserOnForm(this, SystemProperty.UserName) == 0)
                {
                    this.Close();
                    // You don't have the right to view this item
                    PCSMessageBox.Show(ErrorCode.MESSAGE_YOU_HAVE_NO_RIGHT_TO_VIEW, MessageBoxIcon.Warning);
                    return;
                }
                foreach (C1DisplayColumn col in dgrdData.Splits[0].DisplayColumns)
                    if (col.DataColumn.DataField != ITM_ProductTable.PRODUCTID_FLD)
                        col.DataColumn.NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
                // store grid layout
                dtbGridLayout = FormControlComponents.StoreGridLayout(dgrdData);
                LoadReportType();
                btnSave.Enabled = false; btnDelete.Enabled = false; btnEdit.Enabled = false;
                btnCopy.Enabled = false;
            }
            catch (PCSException ex)
            {
                // Displays the error message if throwed from PCSException.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // Displays the error message if throwed from system.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    // Log error message into log file.
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    // Show message if logger has an error.
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
        private void LoadReportType()
        {
            //_items.Add(new ColumnChartItem(Color.DarkBlue, "Column 2", 10.7m));
            _items.Add(new ItemVO(0, "Chuyển số liệu sang bảng AA"));
            _items.Add(new ItemVO(1, "Convert All"));
            _items.Add(new ItemVO(2, "S1: Import to ITM_PRODUCT"));

            _items.Add(new ItemVO(3, "S2: Import Routing")); // Ma HHNB
            _items.Add(new ItemVO(4, "S3: Update Item co duoi LVT, VCNB bang Vat lieu phu, ProductTypeID=3"));//NN_KHAC

            _items.Add(new ItemVO(5, "S4: Insert BOM"));
            _items.Add(new ItemVO(6, "S5: Insert BOM Nguyenvatlieu"));
            _items.Add(new ItemVO(7, "S6: BOM THEO LevelsS"));
            _items.Add(new ItemVO(8, "S7: Đổi chủng loại"));


            //_items.Add(new ItemVO(4, "S3: Update Item co duoi LVT, VCNB bang Vat lieu phu, ProductTypeID=3"));//NN_KHAC

            if (cboReportType.Row == 0) // no contain data
            {
                if (_items.Count > 0)
                {
                    cboReportType.DataSource = _items;
                    cboReportType.ValueMember = "Values";

                    cboReportType.DisplayMember = "Description";
                }
            }
            //cboReportType.Va;
            cboReportType.SelectedIndex = -1;
            //cboReportType.Width = 300;
            cboReportType.Splits[0].DisplayColumns["Values"].Width = 50;
            cboReportType.Splits[0].DisplayColumns["Description"].Width = 250;

            //cboReportType.Text = "";
        }
        private void btnOpenFileDlg_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnOpenFileDlg_Click()";
            try
            {
                OpenFileDialog f = new OpenFileDialog();
                f.Filter = "Excel files | *.xls";
                f.InitialDirectory = Application.ExecutablePath;

                if (f.ShowDialog() == DialogResult.OK)
                    if (f.FileName != null && f.CheckFileExists == true)
                    {
                        strExcelFilename = f.FileName;
                        txtFileName.Text = f.FileName;
                        RetrieveSheetnames();
                        if (this.cboSheetnames.Items.Count > 0)
                            cboSheetnames.SelectedIndex = 0;
                    }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }
        private void RetrieveSheetnames()
        {
            this.cboSheetnames.Items.Clear();

            if (objExcelReader != null)
            {
                objExcelReader.Dispose();
                objExcelReader = null;
            }

            objExcelReader = new ExcelReader();
            objExcelReader.ExcelFilename = strExcelFilename;
            objExcelReader.Headers = false;
            objExcelReader.MixedData = true;
            string[] sheetnames = this.objExcelReader.GetExcelSheetNames();
            this.cboSheetnames.Items.AddRange(sheetnames);
        }

        private void InitExcel(ref ExcelReader exr)
        {
            //Excel must be open
            if (exr == null)
            {
                exr = new ExcelReader();
                exr.ExcelFilename = strExcelFilename;
                exr.Headers = false;
                exr.MixedData = true;
            }
            if (dtbData == null) dtbData = new DataTable("par");
            exr.KeepConnectionOpen = true;

            //Check excel sheetname is selected
            if (this.cboSheetnames.SelectedIndex > -1)
                exr.SheetName = this.cboSheetnames.Text;
            else
                throw new PCSException(ErrorCode.MESSAGE_SELECT_SHEET, string.Empty, null);

            //Set excel sheet range
            exr.SheetRange = this.txtRange.Text;
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnGetData_Click()";
            try
            {
                Cursor = Cursors.WaitCursor;
                InitExcel(ref objExcelReader);
                dtbData = objExcelReader.GetTable("A1");
                dtbData.DefaultView.Sort = "F1";
                dgrdData.DataSource = dtbData;
                FormControlComponents.RestoreGridLayout(dgrdData, dtbGridLayout);

                objExcelReader.Close();
                objExcelReader.Dispose();
                objExcelReader = null;
                Cursor = Cursors.Default;
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnGetData_Click()";
            try
            {
                Cursor = Cursors.WaitCursor;
                InitExcel(ref objExcelReader);
                dtbData = objExcelReader.GetTable("A1");
                dtbData.DefaultView.Sort = "F1";
                dgrdData.DataSource = dtbData;
                FormControlComponents.RestoreGridLayout(dgrdData, dtbGridLayout);

                objExcelReader.Close();
                objExcelReader.Dispose();
                objExcelReader = null;
                Cursor = Cursors.Default;
                btnSave.Enabled = true;
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void btnSelectCode_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnSelectCode_Click()";
            try
            {
                DataRowView dresult = FormControlComponents.OpenSearchForm("ITM_ValueForWorkingTimeMaster", "TenSP", txtGoodsCode.Text.Trim(), null, true);
                if (dresult != null)
                {
                    //              DataTable dtbGridDesign = FormControlComponents.StoreGridLayout(dgrdData);
                    txtGoodsCode.Text = dresult["TenSP"].ToString();
                    txtGoodsCode.Tag = dresult["ValueForWorkingTimeMasterID"];
                    txtGoodsName.Text = dresult["MaHieu"].ToString();
                    ValueForWorkingTimeBO objBO = new ValueForWorkingTimeBO();
                    dset = objBO.GetData(Convert.ToInt32(txtGoodsCode.Tag));
                    dgrdData.DataSource = dset.Tables[0];
                    btnEdit.Enabled = true; btnDelete.Enabled = true; btnSave.Enabled = false;
                    btnCopy.Enabled = true;
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            mFormAction = EnumAction.Add;
            BindDataGrid(-1);
            dgrdData.AllowAddNew = true;
            dgrdData.AllowUpdate = true;
            SetEnableButtons();
            ClearText();
            btnSelectCode.Enabled = true; cboReportType.Enabled = true;
        }
        #region Code Basic
        private void BindDataGrid(int iMaserID)
        {
            ValueForWorkingTimeBO objBO = new ValueForWorkingTimeBO();
            dset = objBO.GetData(iMaserID);
            dgrdData.DataSource = dset.Tables[0];

            dtbGridLayout = FormControlComponents.StoreGridLayout(dgrdData);
            dgrdData.DataSource = dset.Tables[0];
            FormControlComponents.RestoreGridLayout(dgrdData, dtbGridLayout);
            //dgrdData.AllowUpdate = true;
            ReadOnlyDataGrid();

        }
        private void ReadOnlyDataGrid()
        {
            dtbGridLayout = FormControlComponents.StoreGridLayout(dgrdData);
            if (dset.Tables.Count > 0)
            {

                dgrdData.DataSource = dset.Tables[0];

            }
            FormControlComponents.RestoreGridLayout(dgrdData, dtbGridLayout);
        }

        private void SetEnableButtons()
        {
            switch (mFormAction)
            {
                case EnumAction.Add:
                    //Disable Buttons
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;

                    //Enable Buttons
                    btnSave.Enabled = true;

                    // btnIn.Enabled = false;
                    break;
                case EnumAction.Delete:
                    btnAdd.Enabled = true;
                    btnSave.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;

                    //btnIn.Enabled = false;

                    break;
                case EnumAction.Edit:
                    //Disable Buttons
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    //btnIn.Enabled = false;

                    //Enable Buttons
                    btnSave.Enabled = true;

                    break;
                case EnumAction.Default:
                    //Disable Buttons
                    btnSave.Enabled = false;
                    btnAdd.Enabled = true;
                    btnDelete.Enabled = false;
                    btnEdit.Enabled = false;

                    // btnIn.Enabled = false;
                    break;
            }
        }

        private void ClearText()
        {

            txtGoodsCode.Text = "";
            txtGoodsName.Text = "";
            txtModel.Text = "";
            txtGoodsCode.Focus();
        }




        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
           
                DataTable dt = dtbData;
                if (dt == null)
                {
                    //Invalid file
                    PCSMessageBox.Show(ErrorCode.MESSAGE_IMPORT_ERROR_READ_FILE, MessageBoxIcon.Error);
                    return;
                }
 
                //dt.Rows[0].Delete();
                //dt.AcceptChanges();
                ITM_ValueForWorkingTimeMaster objMaster = new ITM_ValueForWorkingTimeMaster();
                objMaster.MaHieu = dt.Rows[0]["F6"] != null ? dt.Rows[0]["F6"].ToString() : DateTime.Now.ToString();
                objMaster.TenSP = dt.Rows[0]["F5"] != null ? dt.Rows[0]["F5"].ToString() : DateTime.Now.ToString();
                dt.Rows[0].Delete();
                dt.AcceptChanges();
                List<ITM_ValueForWorkingTimeDetail> listDetail = new List<ITM_ValueForWorkingTimeDetail>();
                foreach (DataRow dr in dt.Rows)
                {
                    ITM_ValueForWorkingTimeDetail objDetail = new ITM_ValueForWorkingTimeDetail();
                    objDetail.MaSo = dr["F2"].ToString();
                    objDetail.C1 = dr["F3"].ToString();
                    objDetail.C2 = dr["F4"].ToString();
                    objDetail.Name = dr["F5"].ToString();
                    objDetail.Code = dr["F6"].ToString();


                    if (dr["F7"] != null && dr["F7"].ToString() != string.Empty)
                        try
                        {
                            objDetail.Levels = Convert.ToInt32(dr["F7"]);
                        }
                        catch { }
                    if (dr["F8"] != null && dr["F8"].ToString() != string.Empty)
                        try
                        {
                            objDetail.Parrent = dr["F8"].ToString();
                        }
                        catch { }

                    objDetail.DVTP = dr["F9"].ToString();

                    if (dr["F10"] != null && dr["F10"].ToString() != string.Empty)
                        try
                        {
                            objDetail.Gqty = Convert.ToInt32(dr["F10"]);
                            //objDetail.SoLuong = Convert.ToInt32(dr[9]);
                        }
                        catch { }


                    objDetail.TenQuyCachVatLieu = dr["F12"].ToString();
                    objDetail.MaVTHH = dr["F13"].ToString();

                    objDetail.UM = dr["F14"] != null ? dr["F14"].ToString() : string.Empty;


                    if (dr["F15"] != null && dr["F15"].ToString() != string.Empty)
                        try
                        {
                            objDetail.Qty = Convert.ToDecimal(dr["F15"]);
                        }
                        catch { }


                    if (dr["F16"] != null && dr["F16"].ToString() != string.Empty)
                        try
                        {
                            objDetail.TrongLuong = Convert.ToDecimal(dr["F16"]);
                        }
                        catch { }


                    if (dr["F17"] != null && dr["F17"].ToString() != string.Empty)
                        try
                        {
                            objDetail.LinhVT = Convert.ToDecimal(dr["F17"]);
                        }
                        catch { }

                    if (dr["F18"] != null && dr["F18"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m1 = Convert.ToDecimal(dr["F18"]);
                        }
                        catch { }
                    if (dr["F19"] != null && dr["F19"].ToString() != string.Empty)
                        try { objDetail.m2 = Convert.ToDecimal(dr["F19"]); }
                        catch { }
                    if (dr["F20"] != null && dr["F20"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m3 = Convert.ToDecimal(dr["F20"]);
                        }
                        catch { }
                    if (dr["F21"] != null && dr["F21"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m4 = Convert.ToDecimal(dr["F21"]);
                        }
                        catch { }
                    if (dr["F22"] != null && dr["F22"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m5 = Convert.ToDecimal(dr["F22"]);
                        }
                        catch { }
                    if (dr["F23"] != null && dr["F23"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m6 = Convert.ToDecimal(dr["F23"]);
                        }
                        catch { }
                    if (dr["F24"] != null && dr["F24"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m7 = Convert.ToDecimal(dr["F24"]);
                        }
                        catch { }
                    if (dr["F25"] != null && dr["F25"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m8 = Convert.ToDecimal(dr["F25"]);
                        }
                        catch { }
                    if (dr["F26"] != null && dr["F26"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m9 = Convert.ToDecimal(dr["F26"]);
                        }
                        catch { }
                    if (dr["F27"] != null && dr["F27"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m10 = Convert.ToDecimal(dr["F27"]);
                        }
                        catch { }
                    if (dr["F28"] != null && dr["F28"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m11 = Convert.ToDecimal(dr["F28"]);
                        }
                        catch { }
                    if (dr["F29"] != null && dr["F29"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m12 = Convert.ToDecimal(dr["F29"]);
                        }
                        catch { }
                    if (dr["F30"] != null && dr["F30"].ToString() != string.Empty)
                        try
                        {
                            objDetail.m13 = Convert.ToDecimal(dr["F30"]);
                        }
                        catch { }

                    if (dr["F31"] != null && dr["F31"].ToString() != string.Empty)
                        try
                        {
                            objDetail.Vc_NoiBo = Convert.ToDecimal(dr["F31"]);
                        }
                        catch { }
                    objDetail.GhiChu = dr["F32"] != null ? dr["F32"].ToString() : string.Empty;
                    listDetail.Add(objDetail);
                }
                ValueForWorkingTimeBO objBO = new ValueForWorkingTimeBO();
                if (objBO.Insert(objMaster, listDetail))
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA, MessageBoxIcon.Information);
                    //DataTable dtbGridDesign = FormControlComponents.StoreGridLayout(dgrdData);
                    dset = objBO.GetData(objMaster.ValueForWorkingTimeMasterID);
                    dgrdData.DataSource = dset.Tables[0];
                    txtGoodsCode.Text = objMaster.MaHieu;
                    txtGoodsName.Text = objMaster.TenSP;
                    txtGoodsCode.Tag = objMaster.ValueForWorkingTimeMasterID;
                    cboReportType.Enabled = true;
                    //FormControlComponents.RestoreGridLayout(dgrdData, dtbGridLayout);
                }
                else
                    PCSMessageBox.Show(ErrorCode.MESSAGE_CANNOT_SAVE_TO_DB, MessageBoxIcon.Error);
            
            }
            catch (Exception)
            {
                MessageBox.Show("File đầu vào có cấu trúc không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtGoodsCode.Tag != null)
            {
                DialogResult enumResult = PCSMessageBox.Show(ErrorCode.MESSAGE_DELETE_RECORD,
                                                             MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (enumResult == DialogResult.Yes)
                {
                    ValueForWorkingTimeBO objBO = new ValueForWorkingTimeBO();
                    objBO.Delete(Convert.ToInt32(txtGoodsCode.Tag));
                    txtGoodsCode.Tag = null; txtGoodsCode.Text = txtGoodsName.Text = string.Empty;
                    dgrdData.DataSource = null;
                }
            }
        }

        private void cboReportType_SelectedValueChanged(object sender, EventArgs e)
        {
            //try
            //{
            ValueForWorkingTimeBO objBO = new ValueForWorkingTimeBO();
            //Chuyển số liệu sang bảng aa
            if (cboReportType.SelectedIndex == 0)
            {
                objBO.RunScript(Convert.ToInt32(txtGoodsCode.Tag));
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // cboReportType.SelectedValue = null;
            }
            //Run all
            if (cboReportType.SelectedIndex == 1)
            {
                objBO.RunScript(Convert.ToInt32(txtGoodsCode.Tag));
                objBO.RunScript2();
                objBO.RunScript3();
                objBO.RunScript4();
                objBO.RunScript5();
                objBO.RunScript6();
                objBO.RunScript7();
                objBO.RunScript8();
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //cboReportType.SelectedIndex = -1;
            }
            //----- Import to ITM_PRODUCT
            if (cboReportType.SelectedIndex == 2)
            {
                objBO.RunScript2();
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //cboReportType.SelectedIndex = -1;
            }
            //---Import Routing%
            if (cboReportType.SelectedIndex == 3)
            {
                objBO.RunScript3();
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //cboReportType.SelectedIndex = -1;

            }
            //---Update Item co duoi LVT, VCNB bang Vat lieu phu, ProductTypeID=3
            if (cboReportType.SelectedIndex == 4)
            {
                objBO.RunScript4();
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //cboReportType.SelectedIndex = -1;
            }
            //----------- Generate BOM
            /*
            Create view v_BOM AS
            select Parent,min(parentnc) as parentnc,Parent as Component,Componentnc
            from (select p1.code Parent,p2.nc parentnc,p1.nc Componentnc from product p1, product p2
            where p1.code=p2.code and p2.nc>p1.nc) as b
            group by Parent,Componentnc
            */
            //--Insert BOM
            if (cboReportType.SelectedIndex == 5)
            {
                objBO.RunScript5();
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //cboReportType.SelectedIndex = -1;
            }
            //----- Insert BOM Nguyenvatlieu
            if (cboReportType.SelectedIndex == 6)
            {
                objBO.RunScript6();
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //cboReportType.SelectedIndex = -1;
            }
            //--- BOM THEO LevelsS
            if (cboReportType.SelectedIndex == 7)
            {
                objBO.RunScript7();
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //cboReportType.SelectedIndex = -1;
            }
            //-- Đổi chủng loại
            if (cboReportType.SelectedIndex == 8)
            {
                objBO.RunScript8();
                MessageBox.Show("Thao tác được thực hiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //cboReportType.SelectedIndex = -1;
            }
            // MessageBox.Show(cboReportType.SelectedIndex.ToString());
            //}
            //catch (Exception)
            //{
            //    MessageBox.Show("Thao tác không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return;
            //}
            
        }

        private void btnCheckItem_Click(object sender, EventArgs e)
        {
            ValueForWorkingTimeBO objBO = new ValueForWorkingTimeBO();
            DataSet dset = objBO.GetListKTMaVTHH();
            if (dset != null && dset.Tables.Count > 0)
            {
                string strCode = "";
                int idem = 0;
                foreach (DataRow dr in dset.Tables[0].Rows)
                {
                    if (idem > 0 && idem < dset.Tables[0].Rows.Count - 1)
                    {
                        strCode += "," + dr["MaVTHH"].ToString();
                    }
                    else
                    {
                        strCode += dr["MaVTHH"].ToString();
                    }
                }
                MessageBox.Show("MaVTHH đã có trong hệ thống: " + strCode, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                MessageBox.Show("Không có sản phẩm nào trong hệ thống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnKiemtratrung_Click(object sender, EventArgs e)
        {
            ValueForWorkingTimeBO objBO = new ValueForWorkingTimeBO();
            DataSet dset = objBO.GetListTrungSP();
            if (dset != null && dset.Tables.Count > 0)
            {
                string strCode = "";
                int idem = 0;
                foreach (DataRow dr in dset.Tables[0].Rows)
                {
                    if (idem > 0 && idem < dset.Tables[0].Rows.Count - 1)
                    {
                        strCode += "," + dr["TT"].ToString();
                    }
                    else
                    {
                        strCode += dr["TT"].ToString();
                    }
                }
                MessageBox.Show("MaVTHH đã có trong hệ thống: " + strCode, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                MessageBox.Show("Không có sản phẩm nào trong hệ thống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            txtGoodsCode.Text = "";
            txtGoodsName.Text = "";
            iCheckCopy = true;
            btnAdd.Enabled = false;
            btnEdit.Enabled = false;
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
